package analyser;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Comparator;
import java.util.List;

import pojo.Customer;
import processor.DataProcessor;
import processor.impl.DataProcessorImpl;
import scanner.DataScanner;
import scanner.impl.DataScannerImpl;
import comparetor.CustomerComparetorByBirth;
import comparetor.CustomerComparetorById;

public class CmdAnalyserImp implements CmdAnalyser{
	private Comparator<Customer> comparator;		//比较器
	private DataProcessor dp;						//输入list<Coustomer>,比较器，format,返回输出xml或数据
	private DataScanner scanner;
	private List<Customer> list;
	private String format;
	
	public CmdAnalyserImp() throws FileNotFoundException {
		format = "xml";
		DataScanner scanner=new DataScannerImpl(new FileInputStream("test.properties"));
		comparator=new CustomerComparetorById();
		List<Customer> list=scanner.scan();
	}

	public void cmdAnalyseAndHandle(String cmd, OutputStream out) throws IOException {
		String[] params = cmd.split(" ");
		
		format = params[1];
		if(params[2].equals("id")){
			comparator = new CustomerComparetorById();
		}
		else{
			comparator = new CustomerComparetorByBirth();
		}
		
		dp=new DataProcessorImpl(list, format);             
		StringBuilder sb=dp.process(comparator);
		
		if(params[0].equals("L")){
			//网络
			out.write(sb.toString().getBytes());
		}
		else if(params[0].equals("O")){
			//文件
		}
		else if(params[0].equals("Q")){
			//退出
		}
	}

	public void cmdAnalyseAndHandle(String cmd) {
		// TODO Auto-generated method stub
		
	}
}
